#include "liteutils.h"

std::string LiteUtils::getCurrentTime(TimeLevelType level)
{
	std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
	std::time_t now_time_t = std::chrono::system_clock::to_time_t(now);
	std::tm* now_tm = std::localtime(&now_time_t);

	char buffer[64];
	strftime(buffer, sizeof(buffer), "%F %T", now_tm);

	switch (level)
	{
		case SECOND_LEVEL:
		{
			return std::string(buffer);
		}
		case MILLISECOND_LEVEL:
		{
			std::chrono::milliseconds ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
			return stringSprintf("%s.%d", buffer, ms.count());
		}
		case MICROSECOND_LEVEl:
		{
			std::chrono::milliseconds ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
			std::chrono::microseconds mcs = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()) % 1000000;
			return stringSprintf("%s:%d.%d", buffer, ms.count(), mcs.count() % 1000);
		}
		case NANOSECOND_LEVEL:
		{
			std::chrono::milliseconds ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
			std::chrono::microseconds mcs = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()) % 1000000;
			std::chrono::nanoseconds ns = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()) % 1000000000;
			return stringSprintf("%s:%d:%d.%d", buffer, ms.count(), mcs.count() % 1000, ns.count() % 1000);
		}
		default:
			break;
	}

	return std::string(buffer);
}